//
// Created by HhL on 2021/7/8.
//
#include "../../../include/heap_utils.h"
#include "../../../include/linkList.h"
#include "../../../include/dynamicArray.h"
#include "../../../include/stack.h"
#include "../../../include/SeqQueue.h"

//测试
struct Person {
    char name[64];
    int age;
};

void test_dynamicArray();

void test_LinkList();

void test_stack();

void test_myQueue();

void myPrintPerson(void* data) {
    struct Person* p = data;
    printf("姓名： %s 年龄：%d\n", p->name, p->age);

}

int myComparePerson(void* data1, void* data2) {
    struct Person* p1 = data1;
    struct Person* p2 = data2;

    return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}

void test03() {


//    test_LinkList();
}

void test_LinkList() {
    //初始化链表
    struct LinkNode* pHeader = initLinkList();

    //遍历链表
    printf("遍历链表结果为：\n");
    foreach_LinkList(pHeader);

    //插入链表
    // 10 1000  2000 20 3000 30  500
    insert_LinkList(pHeader, 20, 1000);
    insert_LinkList(pHeader, 20, 2000);
    insert_LinkList(pHeader, -1, 500);
    insert_LinkList(pHeader, 30, 3000);
    printf("插入链表后，遍历链表结果为：\n");
    foreach_LinkList(pHeader);


    //删除链表
    // 10  20  30  500
    delete_LinkList(pHeader, 2000);
    delete_LinkList(pHeader, 3000);
    delete_LinkList(pHeader, 1000);
    delete_LinkList(pHeader, -1);
    printf("删除链表后，遍历链表结果为：\n");
    foreach_LinkList(pHeader);

    //清空链表
    clear_LinkList(pHeader);
    printf("清空链表后，遍历链表结果为：\n");
    insert_LinkList(pHeader, 111, 111);
    insert_LinkList(pHeader, 222, 222);
    insert_LinkList(pHeader, 333, 333);
    foreach_LinkList(pHeader);

    //销毁链表
    destroy_LinkList(pHeader);
    pHeader = NULL;
}

void test_dynamicArray() {
//初始化动态数组
    struct dynamicArray* array = init_DynamicArray(5);

    //准备数据
    struct Person p1 = {"亚瑟", 18};
    struct Person p2 = {"妲己", 20};
    struct Person p3 = {"安琪拉", 19};
    struct Person p4 = {"凯", 21};
    struct Person p5 = {"孙悟空", 999};
    struct Person p6 = {"李白", 999};

    printf("插入数据前： 容量：%d  大小：%d\n", array->m_capacity, array->m_size);

    //插入数据
    insert_DynamicArray(array, 0, &p1);
    insert_DynamicArray(array, 0, &p2);
    insert_DynamicArray(array, 1, &p3);
    insert_DynamicArray(array, 0, &p4);
    insert_DynamicArray(array, -1, &p5);
    insert_DynamicArray(array, 2, &p6);

    // 凯 妲己 李白 安琪拉  亚瑟  孙悟空

    //遍历数据
    foreach_DynamicArray(array, myPrintPerson);


    printf("插入数据后： 容量：%d  大小：%d\n", array->m_capacity, array->m_size);


    //测试删除 按位置删除
    removeByPos_DynamicArray(array, 2);
    printf("---------------------\n");
    foreach_DynamicArray(array, myPrintPerson);
    printf("删除数据后： 容量：%d  大小：%d\n", array->m_capacity, array->m_size);

    struct Person p = {"亚瑟", 18};
    removeByValue_DynamicArray(array, &p, myComparePerson);
    printf("---------------------\n");
    foreach_DynamicArray(array, myPrintPerson);
    printf("删除数据后： 容量：%d  大小：%d\n", array->m_capacity, array->m_size);


    //array->pAddr = NULL;
    //array->m_capacity = 0;
    //array->m_size = 0;

    //销毁数组
    destroy_DynamicArray(array);
    array = NULL;
}

void test01() {
    //创建节点
    struct LinkNode node1 = {10, NULL};
    struct LinkNode node2 = {20, NULL};
    struct LinkNode node3 = {30, NULL};
    struct LinkNode node4 = {40, NULL};
    struct LinkNode node5 = {50, NULL};

    //建立关系
    node1.next = &node2;
    node2.next = &node3;
    node3.next = &node4;
    node4.next = &node5;

    //遍历链表
    struct LinkNode* pCurrent = &node1;

    while (pCurrent != NULL) {
        printf("%d\n", pCurrent->num);
        pCurrent = pCurrent->next;
    }


}

void test02() {
    //创建节点
    struct LinkNode* node1 = malloc(sizeof(struct LinkNode));
    struct LinkNode* node2 = malloc(sizeof(struct LinkNode));
    struct LinkNode* node3 = malloc(sizeof(struct LinkNode));
    struct LinkNode* node4 = malloc(sizeof(struct LinkNode));
    struct LinkNode* node5 = malloc(sizeof(struct LinkNode));

    //给数据域赋值
    node1->num = 100;
    node2->num = 200;
    node3->num = 300;
    node4->num = 400;
    node5->num = 500;

    //建立关系
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = node5;
    node5->next = NULL;

    //遍历链表
    struct LinkNode* pCurrent = node1;
    while (pCurrent != NULL) {
        printf("%d\n", pCurrent->num);
        pCurrent = pCurrent->next;
    }

    free(node1);
    free(node2);
    free(node3);
    free(node4);
    free(node5);
    node1 = NULL;
    node2 = NULL;
    node3 = NULL;
    node4 = NULL;
    node5 = NULL;
}

void test_stack() {
//初始化栈
    SeqStack myStack = init_SeqStack();

    //创建数据
    struct Person p1 = {"aaa", 10};
    struct Person p2 = {"bbb", 20};
    struct Person p3 = {"ccc", 30};
    struct Person p4 = {"ddd", 40};
    struct Person p5 = {"eee", 50};

    //入栈
    push_SeqStack(myStack, &p1);
    push_SeqStack(myStack, &p2);
    push_SeqStack(myStack, &p3);
    push_SeqStack(myStack, &p4);
    push_SeqStack(myStack, &p5);

    printf("栈的元素个数为：%d\n", size_SeqStack(myStack));

    while (isEmpty_SeqStack(myStack) == 0) //栈不为空，查看栈顶元素，出栈
    {
        struct Person* p = top_SeqStack(myStack);
        printf("姓名：%s 年龄：%d\n", p->name, p->age);

        //出栈
        pop_SeqStack(myStack);
    }

    printf("栈的元素个数为：%d\n", size_SeqStack(myStack));

    //销毁栈
    destroy_SeqStack(myStack);
}

void test_myQueue() {
//初始化队列
    seqQueue myQueue = init_SeqQueue();

    //准备数据
    struct Person p1 = {"aaa", 10};
    struct Person p2 = {"bbb", 20};
    struct Person p3 = {"ccc", 30};
    struct Person p4 = {"ddd", 40};


    //入队
    push_SeqQueue(myQueue, &p1);
    push_SeqQueue(myQueue, &p2);
    push_SeqQueue(myQueue, &p3);
    push_SeqQueue(myQueue, &p4);
    printf("队列大小为：%d\n", size_SeqQueue(myQueue));
    while (isEmpty_SeqQueue(myQueue) == 0) {
        //访问队头
        struct Person* pFront = front_SeqQueue(myQueue);
        printf("队头元素 -- 姓名：%s  年龄： %d\n", pFront->name, pFront->age);
        //访问队尾
        struct Person* pBack = back_SeqQueue(myQueue);
        printf("队尾元素 -- 姓名：%s  年龄： %d\n", pBack->name, pBack->age);
        //出队
        pop_SeqQueue(myQueue);
    }

    printf("队列大小为：%d\n", size_SeqQueue(myQueue));

    //销毁队列
    destroy_SeqQueue(myQueue);
}

int main() {
//    test01();
//    test02();
//    test03();
//    test_dynamicArray();
//    test_stack();
    test_myQueue();
    printf("score = %d\n", 1);
    return 0;
}



